package tk.mybatis.springboot.share.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类 yyyy MM dd HH mm ss 年月日时分秒 title:TimeUtil form:Mr_xing
 * 
 * @author Xing
 * @date 2016年4月26日 下午3:15:44
 */
public class DateUtils {
	public static final SimpleDateFormat formatter_ymd = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat formatter_ymdh = new SimpleDateFormat("yyyy-MM-dd HH");
	public static final SimpleDateFormat formatter_ymdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat formatter_ymdhm = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static final SimpleDateFormat formatter_ymdhmsf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

	public static final SimpleDateFormat formatter_en6 = new SimpleDateFormat("yyyyMMddHHmmss");
	public static final SimpleDateFormat formatter_en3 = new SimpleDateFormat("yyyyMMdd");

	public static final SimpleDateFormat formatter_html_3 = new SimpleDateFormat("yyyy/MM/dd");

	public static final String FORMATTER_YMDH = "yyyyMMdd";

	/** 1分钟 */
	public static final Long time_min = (long) (1 * 60 * 1000);
	/** 1小时 */
	public static final Long time_hours = (long) (1 * 60 * time_min);
	/** 2小时 */
	public static final int tow_day =  (int)(2 * time_hours);
	
	/** 1天 */
	public static final Long time_day = (long) (1 * 24 * time_hours);
	/** 1月(30天) */
	public static final Long time_month = (long) (1 * 30 * time_day);

	/** 1天秒数 */
	public static final int datetime_1day = 86400;
	/** 1月秒数 */
	public static final int datetime_1month = datetime_1day * 30;

	/**
	 * 得到当前时间的Timestamp对象 sql包
	 * 
	 * @return
	 */
	public static Timestamp getThisTimestamp() {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		return timestamp;
	}

	/**
	 * 格式化日期 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param obj
	 * @return
	 */
	public static String Date_Str_6(Date obj) {
		StringBuffer date = new StringBuffer("");
		try {
			if (obj != null) {
				date.append(formatter_ymdhms.format(obj));
			}
		} catch (Exception e) {}
		return date.toString();
	}

	/**
	 * 格式化日期 yyyy-MM-dd HH:mm
	 * 
	 * @param obj
	 * @return
	 */
	public static String Date_Str_5(Date obj) {
		StringBuffer date = new StringBuffer("");
		try {
			if (obj != null) {
				date.append(formatter_ymdhm.format(obj));
			}
		} catch (Exception e) {}
		return date.toString();
	}

	/**
	 * 格式化日期，格式化为yyyy-MM-dd
	 * 
	 * @param obj
	 * @return
	 */
	public static String Date_Str_3(Date obj) {
		StringBuffer date = new StringBuffer("");
		try {
			if (obj != null) {
				date.append(formatter_ymd.format(obj));
			}
		} catch (Exception e) {}
		return date.toString();
	}

	/**
	 * 格式化日期
	 * 
	 * @param obj
	 * @return
	 */
	public static String Date_simp(Date obj, SimpleDateFormat simp) {
		StringBuffer date = new StringBuffer("");
		try {
			if (obj != null) {
				date.append(simp.format(obj));
			}
		} catch (Exception e) {}
		return date.toString();
	}

	/**
	 * 根据传入字符串及正则表达式来得到日期 yyyy MM dd HH mm ss 年月日时分秒
	 * 
	 * @param str
	 *            时间
	 * @param format
	 *            格式
	 * @return
	 */
	public static Date Str_Date(String str, String format) {
		Date date = null;
		try {
			if (StringUtils.isNotEmpty(str)) {
				SimpleDateFormat simp = new SimpleDateFormat(format);
				date = simp.parse(str);
			}
		} catch (Exception e) {}
		return date;
	}

	/**
	 * 字符串转化为date对象,可区分yyyy-MM-dd和yyyy-MM-dd HH:mm:ss ss：SSS yyyy/mm/dd ，
	 * 
	 * @param parameter
	 * @return
	 */
	public static Date Str_Date(String parameter) {
		Date date = null;
		try {
			if (StringUtils.isNotEmpty(parameter)) {
				if (parameter.matches("\\d{4}-\\d{2}-\\d{2}")) {
					date = formatter_ymd.parse(parameter);
				} else if (parameter.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
					date = formatter_ymdhms.parse(parameter);
				} else if (parameter.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.[0]")) {
					date = formatter_ymdhms.parse(parameter);
				} else if (parameter.matches("\\d{4}/\\d{2}/\\d{2}")) {
					date = formatter_html_3.parse(parameter);
				}

			}
		} catch (Exception e) {}
		return date;
	}

	/**
	 * 得到当前时间的格式化对象 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getNowTime_3() {
		StringBuffer date = new StringBuffer("");
		try {
			date.append(Date_Str_3(new Date()));
		} catch (Exception e) {

		}
		return date.toString();
	}

	/**
	 * 得到当前时间的格式化对象 yyyy
	 * 
	 * @return
	 */
	public static String getNowTime_y() {
		StringBuffer date = new StringBuffer("");
		try {
			date.append(Date_simp(new Date(), new SimpleDateFormat("yyyy")));
		} catch (Exception e) {

		}
		return date.toString();
	}

	/**
	 * 得到当前时间的格式化对象 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getNowTime_6() {
		StringBuffer date = new StringBuffer("");
		try {
			date.append(Date_Str_6(new Date()));
		} catch (Exception e) {

		}
		return date.toString();
	}

	/**
	 * 得到当前时间的格式化对象 yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getNowTime_7() {
		StringBuffer date = new StringBuffer("");
		try {
			date.append(Date_simp(new Date(), formatter_ymdhmsf));
		} catch (Exception e) {

		}
		return date.toString();
	}

	/**
	 * 增加1天的时间
	 * @param date
	 * @return
	 */
	public static Date date_up_1_day(Date date) {
		Date day_1 = new Date(date.getTime() + DateUtils.time_day);
		return day_1;
	}

	/**
	 * 展示时间
	 * 
	 * @param date
	 * @return
	 */
	public static String showTime(Date date) {
		Date now = new Date();
		long l = now.getTime() - date.getTime();
		long day = l / (24 * 60 * 60 * 1000);
		if (day > 0)
			return new SimpleDateFormat("MM月dd日").format(date) + "发布";
		long hour = (l / (60 * 60 * 1000) - day * 24);
		if (hour > 0)
			return hour + "小时前发布";
		long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		if (min > 0)
			return min + "分钟前发布";
		return "刚刚发布";
	}

	public static void main(String[] args) {
		String s = "2016-11-1111";
		String f = "yyyy-MM-ddHH";
		System.out.println(Str_Date(s, f));
	}
}

